package com.athuahua.spzx.service.impl;

import com.alibaba.fastjson2.JSON;
import com.athuahua.spzx.entity.Region;
import com.athuahua.spzx.mapper.RegionMapper;
import com.athuahua.spzx.utils.BaseContext;
import com.athuahua.spzx.vo.CartInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.athuahua.spzx.entity.UserAddress;
import com.athuahua.spzx.service.UserAddressService;
import com.athuahua.spzx.mapper.UserAddressMapper;
import jakarta.annotation.Resource;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author huahua
 * @description 针对表【user_address(用户地址表)】的数据库操作Service实现
 * @createDate 2024-08-17 23:04:51
 */
@Service
public class UserAddressServiceImpl extends ServiceImpl<UserAddressMapper, UserAddress>
        implements UserAddressService {

    @Resource
    private UserAddressMapper userAddressMapper;

    @Resource
    private RegionMapper regionMapper;


    @Override
    public List<UserAddress> findUserAddressList() {
        List<UserAddress> userAddresses = userAddressMapper.selectList(new LambdaQueryWrapper<UserAddress>()
                .eq(UserAddress::getUserId, BaseContext.getCurrentId().intValue())
                .orderByDesc(UserAddress::getIsDefault));
        return userAddresses;
    }

    @Override
    public UserAddress findUserAddressById(Integer parentCode) {
        return userAddressMapper.selectOne(new LambdaQueryWrapper<UserAddress>()
                .eq(UserAddress::getProvinceCode, parentCode));
    }

    @Override
    public void iUpdateById(UserAddress userAddress) {
        //处理地址
        userAddress.setFullAddress(getFullAddress(userAddress));
        //处理默认地址
        setIsDefault(userAddress);


        userAddressMapper.updateById(userAddress);
    }

    @Override
    public void iSave(UserAddress userAddress) {
        //处理地址
        userAddress.setFullAddress(getFullAddress(userAddress));
        userAddress.setUserId(BaseContext.getCurrentId().intValue());
        //处理默认地址
        setIsDefault(userAddress);

        userAddressMapper.insert(userAddress);
    }

    private String getFullAddress(UserAddress userAddress){
        String provinceCode = userAddress.getProvinceCode();
        String cityCode = userAddress.getCityCode();
        String districtCode = userAddress.getDistrictCode();

        Region province = regionMapper.selectOne(new LambdaQueryWrapper<Region>()
                .eq(Region::getCode, provinceCode));
        Region city = regionMapper.selectOne(new LambdaQueryWrapper<Region>()
                .eq(Region::getCode, cityCode));
        Region district = regionMapper.selectOne(new LambdaQueryWrapper<Region>()
                .eq(Region::getCode, districtCode));


        return province.getName() + city.getName() + district.getName() + userAddress.getAddress();
    }


    private void setIsDefault(UserAddress userAddress) {
        Integer isDefault = userAddress.getIsDefault();
        if (isDefault == 1) {
            List<UserAddress> userAddresses = userAddressMapper.selectList(new LambdaQueryWrapper<UserAddress>()
                    .eq(UserAddress::getUserId, BaseContext.getCurrentId().intValue()));
            userAddresses.forEach(item -> {
                if (item.getIsDefault() == 1) {
                    item.setIsDefault(0);
                    userAddressMapper.updateById(item);
                }
            });
        }
    }
}




